require 'network/logon_handler'
require 'network/character_handler'

class String
  def sanitize
    #i also need to strip out backspace characters.. or bad things happen..
    self.strip.downcase.gsub('\r\n' , '')
  end
end


class TelnetHandler  
   
  def initialize(connection)
    @connection = connection
      
    @logon_handler = LogonHandler.new(@connection, self)
    @character_handler = CharacterHandler.new(@connection)

    @handle = []
    @handle.push :logon_handler
    
      @handlers = {}
      
      @buffer = ""
      
      @handlers[:logon_handler] = lambda do |data|
        @logon_handler.handle data
      end
  
      @handlers[:character_handler] = lambda do |data|
        @character_handler.handle data
      end
  
      #@handlers[:game_handler] = lambda do |data|
      #  @game_handler.handle data
      #end
      
    end
    
    def push_handler(handle)
      @handle.push handle
    end
    
    def pop_handler
      @handle.pop
    end
    
    def handle data
      
    @buffer << data
      if @buffer.include?("\r\n")    
        @handlers[@handle.last].call @buffer.sanitize
        @buffer = ""
      end
    end
    
        
  end